import api from "../../api/index.js";
const wxbarcode = require('wxbarcode')

Page({
  data: {
    orderId: null, //订单id
    payOrderNo: null, //支付id
    company:null,
  },
  onLoad(e) {
    getApp().initLanguage(this)
    wx.setNavigationBarTitle({
      title: this.data.$t.order.detail,
    })
    this.setData({
      orderId: e.id,
      payOrderNo: e.payOrderNo,
    })
    if (e.payOrderNo) {
      this.payLogs()
    }
  },
  onShow: function (options) {
    this.orderDetail()
  },
  async payLogs() {
    
  },
  async orderDetail() {
    api.orderDetail({
      method: "GET",
      id: this.data.orderId,
    }).then((res) => {
      // 绘制核销码
      if (res.data) {
        wxbarcode.qrcode('qrcode', res.data.id, 400, 400);
      }
      this.setData({
        orderDetail: res.data
      })
      if (res.data.companyId) {
        this.shopSubdetail(res.data.companyId) 
      }
    })
  },
  async shopSubdetail(companyId) {
    api.getCompanyById({
      data:{
        id:companyId
      }
    }).then((res)=>{
      this.setData({
        company: res.data
      })
    })
  },
  async toPayTap() {
    // // 立即支付
    // let res = await WXAPI.userAmount(wx.getStorageSync('token'))
    // if (res.code != 0) {
    //   wx.showToast({
    //     title: res.msg,
    //     icon: 'none'
    //   })
    //   return
    // }
    // const balance = res.data.balance // 当前用户的余额
    // let needPay = this.data.orderDetail.orderInfo.amountReal*1 - balance*1
    // needPay = needPay.toFixed(2)
    // if (needPay <= 0) {
    //   // 余额足够
    //   WXAPI.orderPay(wx.getStorageSync('token'), this.data.orderDetail.orderInfo.id).then(res => {
    //     wx.showToast({
    //       title: this.data.$t.asset.success,
    //       icon: 'success'
    //     })
    //     this.orderDetail();
    //   })
    // } else {
    //   // 微信支付
    //   this.setData({
    //     paymentShow: true,
    //     money: needPay,
    //     orderId: this.data.orderDetail.orderInfo.id,
    //     nextAction: {
    //       type: 0,
    //       id: this.data.orderDetail.orderInfo.id
    //     }
    //   })
    // }
    // 微信支付
    this.setData({
      paymentShow: true,
      money: this.data.orderDetail.receivableMoney,
      orderId: this.data.orderDetail.id,
      nextAction: {
        type: 0,
        id: this.data.orderDetail.id
      }
    })
  },
  // 删除订单
  deleteOrder: function (e) {
    const _this = this;
    wx.showModal({
      confirmText: this.data.$t.common.confirm,
      cancelText: this.data.$t.common.cancel,
      content: this.data.$t.order.deleteProfile,
      success: function (res) {
        if (res.confirm) {
          api.orderDel({
            method: "POST",
            data: {
              id: _this.data.orderId
            },
          }).then(() => {
            let todoListPayOrder = wx.getStorageSync('todoList.payOrder')
            if (todoListPayOrder) {//兼容其他来源 没有待办的支付单
              todoListPayOrder = todoListPayOrder.filter(item => item.payForId != _this.data.orderId);
              wx.setStorageSync('todoList.payOrder', todoListPayOrder)
            }
          }).then(() => {
            //删除后？？？
            wx.navigateBack({delta: 1 })
          })
        }
      }
    })
  },
  paymentOk(e) {
    console.log(e.detail); // 这里是组件里data的数据
    this.setData({
      paymentShow: false
    })
    wx.redirectTo({
      url: '/pages/all-orders/index',
    })
  },
  paymentCancel() {
    this.setData({
      paymentShow: false
    })
  },
  callshop() {
    
    wx.makePhoneCall({
      phoneNumber: 18702156119,
    })
  },
  wuliuDetailsTap: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/wuliu/index?id=" + orderId
    })
  },
  confirmBtnTap(e) {
    let that = this;
    let orderId = this.data.orderId;
    wx.showModal({
      confirmText: this.data.$t.common.confirm,
      cancelText: this.data.$t.common.cancel,
      content: this.data.$t.order.askConfirm,
      success: function (res) {
        if (res.confirm) {
          WXAPI.orderDelivery(wx.getStorageSync('token'), orderId).then(function (res) {
            if (res.code == 0) {
              that.onShow();
            }
          })
        }
      }
    })
  },
  submitReputation: function (e) {
    let that = this;
    let postJsonString = {};
    postJsonString.token = wx.getStorageSync('token');
    postJsonString.orderId = this.data.orderId;
    let reputations = [];
    let i = 0;
    while (e.detail.value["orderGoodsId" + i]) {
      let orderGoodsId = e.detail.value["orderGoodsId" + i];
      let goodReputation = e.detail.value["goodReputation" + i];
      let goodReputationRemark = e.detail.value["goodReputationRemark" + i];

      let reputations_json = {};
      reputations_json.id = orderGoodsId;
      reputations_json.reputation = goodReputation;
      reputations_json.remark = goodReputationRemark;

      reputations.push(reputations_json);
      i++;
    }
    postJsonString.reputations = reputations;
    WXAPI.orderReputation({
      postJsonString: JSON.stringify(postJsonString)
    }).then(function (res) {
      if (res.code == 0) {
        that.onShow();
      }
    })
  }
})